package com.xauto.pioneer.appuser.activity.mine;

import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.widget.EditText;

import com.xauto.pioneer.appuser.R;
import com.xauto.pioneer.appuser.activity.BaseActivity;
import com.xauto.pioneer.appuser.http.BaseLHttpHandler;
import com.xauto.pioneer.appuser.http.LHttpLib;
import com.xauto.pioneer.appuser.model.ClientSetting;
import com.xauto.pioneer.appuser.model.JSONStatus;
import com.xauto.pioneer.appuser.util.StringUtils;
import com.xauto.pioneer.appuser.util.ToastUtil;
import com.xauto.pioneer.appuser.view.PromptDialog;
import com.xauto.pioneer.appuser.view.TopBar;

import net.tsz.afinal.FinalDb;

import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

/***
 * 设置中心-意见反馈页面
 * @author Lemon Sun 12 Feb 19:27
 */
public class SettingFeedbackActivity extends BaseActivity {

    @Bind(R.id.topBar) TopBar topBar;
    /** 输入-意见反馈*/
    @Bind(R.id.etFeedback) EditText etFeedback;
    private PromptDialog promptDialog;

    private String telNumber = "";
    @Override
    protected int getContentView() {
        return R.layout.ac_feedback;
    }

    @Override
    protected void initView() {
        topBar.showView(TopBar.TITLE, TopBar.LEFT_IMAGE_BTN);
        topBar.setText(TopBar.TITLE, getString(R.string.top_bar_feedback));
        topBar.setImage(TopBar.LEFT_IMAGE_BTN, R.mipmap.ic_common_left);
        topBar.setLeftOnClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        initTelNumber();
        promptDialog = new PromptDialog(mContext, getString(R.string.notice), getString(R.string.prompt_call_mobile),
                getString(R.string.confirm), getString(R.string.cancel),getString(R.string.dialogpic_common), new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //拨打电话
                promptDialog.dismiss();
                //用intent启动拨打电话
                Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"+telNumber));
                startActivity(intent);
            }
        }, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //取消
                promptDialog.dismiss();
            }
        });
    }
    private void initTelNumber(){
        FinalDb finalDb = FinalDb.create(mContext);
        List<ClientSetting> client = finalDb.findAll(ClientSetting.class);
        if (client != null && client.size() >0){
            telNumber = client.get(client.size()- 1).getContact_number();
        }
    }

    @Override
    protected void initData() {

    }

    @OnClick({R.id.tvFeedbackCallMobile, R.id.tvFeedbackSubmit})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tvFeedbackCallMobile:
                //拨打电话
                promptDialog.show();
                break;
            case R.id.tvFeedbackSubmit:
                //提交意见反馈
                String feedback = etFeedback.getText().toString();
                if(StringUtils.isBlank(feedback)) {
                    ToastUtil.showShort(mContext, "反馈问题不能为空");
                    return;
                }
                submitFeedback(feedback);
                break;
        }
    }

    /**
     * 提交意见反馈信息
     */
    private void submitFeedback(String feedback){
        new LHttpLib().getFeedback(mContext,memberId,feedback,new BaseLHttpHandler(mContext,true,true){
            @Override
            public void onSuccess(JSONStatus jsonStatus) {
                super.onSuccess(jsonStatus);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        ToastUtil.showShort(mContext, "意见反馈提交成功");
                        finish();
                    }
                });

            }
        });
    }
    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }
}
